﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using Microsoft.Win32;
using Ocmf.Types;

namespace Ocmf.Forms
{
    public partial class EditImageActionForm : Form
    {
        public Card Card { get; set; }
        
        public EditImageActionForm(Card card)
        {
            InitializeComponent();

            //create image list
            listViewImageLibrary.LargeImageList = new ImageList(){ImageSize = new Size(128,128), ColorDepth = ColorDepth.Depth32Bit};

            //show preset card in preview
            Card = card;
            
            //refresh the view
            refreshListViewImageLibrary();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            Card.CardKey = Card.SelectedImageKey = listViewImageLibrary.SelectedItems[0].ImageKey;
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void listViewImageLibrary_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Card.CardKey = Card.SelectedImageKey = listViewImageLibrary.SelectedItems[0].ImageKey;
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void browseButton_Click(object sender, EventArgs e)
        {
            var openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Image Files (*.bmp, *.jpg, *.gif, *.png)|*.bmp;*.jpg;*.gif;*.png";
            openFileDialog.Title = "Import Images...";
            if(openFileDialog.ShowDialog() == DialogResult.OK)
            {
                Image.FromFile(openFileDialog.FileName).Save(Path.Combine(LocalSettings.ApplicationImageFolder, Path.GetFileName(openFileDialog.FileName)));
                //refresh the view
                refreshListViewImageLibrary();
            }
        }

        private void refreshListViewImageLibrary()
        {
            //import library
            if (Directory.Exists(LocalSettings.ApplicationImageFolder))
            {
                listViewImageLibrary.Clear();

                //PNG Files
                var imageFileNames = Directory.GetFiles(LocalSettings.ApplicationImageFolder, "*.png");
                foreach (var imageFileName in imageFileNames)
                {
                    listViewImageLibrary.LargeImageList.Images.Add(Path.GetFileName(imageFileName), Image.FromFile(imageFileName));
                    listViewImageLibrary.Items.Add(Path.GetFileName(imageFileName), Path.GetFileName(imageFileName));
                    if (!Card.TreeView.ImageList.Images.ContainsKey(Path.GetFileName(imageFileName)))
                        Card.TreeView.ImageList.Images.Add(Path.GetFileName(imageFileName),Image.FromFile(imageFileName));
                }

                //JPG Files
                imageFileNames = Directory.GetFiles(LocalSettings.ApplicationImageFolder, "*.jpg");
                foreach (var imageFileName in imageFileNames)
                {
                    listViewImageLibrary.LargeImageList.Images.Add(Path.GetFileName(imageFileName), Image.FromFile(imageFileName));
                    listViewImageLibrary.Items.Add(Path.GetFileName(imageFileName), Path.GetFileName(imageFileName));
                    if (!Card.TreeView.ImageList.Images.ContainsKey(Path.GetFileName(imageFileName)))
                        Card.TreeView.ImageList.Images.Add(Path.GetFileName(imageFileName), Image.FromFile(imageFileName));
                }
                //BMP Files
                imageFileNames = Directory.GetFiles(LocalSettings.ApplicationImageFolder, "*.bmp");
                foreach (var imageFileName in imageFileNames)
                {
                    listViewImageLibrary.LargeImageList.Images.Add(Path.GetFileName(imageFileName), Image.FromFile(imageFileName));
                    listViewImageLibrary.Items.Add(Path.GetFileName(imageFileName), Path.GetFileName(imageFileName));
                    if (!Card.TreeView.ImageList.Images.ContainsKey(Path.GetFileName(imageFileName)))
                        Card.TreeView.ImageList.Images.Add(Path.GetFileName(imageFileName), Image.FromFile(imageFileName));
                }
                //GIF Files
                imageFileNames = Directory.GetFiles(LocalSettings.ApplicationImageFolder, "*.gif");
                foreach (var imageFileName in imageFileNames)
                {
                    listViewImageLibrary.LargeImageList.Images.Add(Path.GetFileName(imageFileName), Image.FromFile(imageFileName));
                    listViewImageLibrary.Items.Add(Path.GetFileName(imageFileName), Path.GetFileName(imageFileName));
                    if (!Card.TreeView.ImageList.Images.ContainsKey(Path.GetFileName(imageFileName)))
                        Card.TreeView.ImageList.Images.Add(Path.GetFileName(imageFileName), Image.FromFile(imageFileName));
                }
            }
        }
    }
}
